package pl.ptemich.rehaesstetica;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;

public class Start {

  private static boolean oddRow = false;
  
  private static final int GROUP_COLUMN_INDEX = 2;
  private static final int SERVICE_NAME_COLUMN_INDEX = 0;
  private static final int SERVICE_PRICE_COLUMN_INDEX = 1;
  private static final int BOLD_COLUMN_INDEX = 3;
  
  /**
   * @param args
   */
  public static void main(String[] args) {
    System.out.println("start import");
    String fileName = "c:\\import.xls";
    String exportOutFilename = "c:\\export.txt";
    try {
      File exportOut = new File(exportOutFilename);
      exportOut.createNewFile();
      PrintWriter writter = new PrintWriter(exportOut);
      HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(new File(fileName)));
      HSSFSheet sheet = workbook.getSheetAt(0);
      Iterator<Row> rowIterator = sheet.rowIterator();
      boolean firstTable = true;
      while (rowIterator.hasNext()) {
        Row row = rowIterator.next();        
        if (row.getCell(GROUP_COLUMN_INDEX) != null) {
          if (!firstTable) {
            inserTableEnd(writter);
          }
          inserTableHeader(writter, row.getCell(SERVICE_NAME_COLUMN_INDEX).getStringCellValue());
          firstTable = false;
        } else {
          String serviceName = row.getCell(SERVICE_NAME_COLUMN_INDEX) != null ? row.getCell(SERVICE_NAME_COLUMN_INDEX).toString() : null;
          String servicePrice = row.getCell(SERVICE_PRICE_COLUMN_INDEX) != null ? row.getCell(SERVICE_PRICE_COLUMN_INDEX).toString() : null;
          boolean bold = row.getCell(BOLD_COLUMN_INDEX) != null && row.getCell(BOLD_COLUMN_INDEX).getStringCellValue().length() != 0 ? true : false;
          insertRow(writter, serviceName, servicePrice, bold);
        }
      }
      inserTableEnd(writter);
      writter.flush();
      writter.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    System.out.println("end");
  }

  
  private static void inserTableHeader(PrintWriter writter, String tableHeader) {
    writter.println("<table class=\"prices\" cellpadding=\"2\" cellspacing=\"0\">");
    writter.println("  <thead>");
    writter.println("    <tr>");
    writter.println("      <th colspan=\"2\"><span>" + tableHeader + "</span></th>");
    writter.println("    </tr>");
    writter.println("  </thead>");
    writter.println("  <tbody>");    
  }
  
  private static void inserTableEnd(PrintWriter writter) {    
    writter.println("  </tbody>");    
    writter.println("</table>");
  }  
  
  private static void insertRow(PrintWriter writter, String serviceName, String servicePrice, boolean bold) {
    String oddRowClass = "";
    if (oddRow) {
      oddRowClass = " class=\"oddRow\"";      
    } 
    oddRow = !oddRow;
    
    String boldStart = "";
    String boldEnd = "";
    if (bold) {
      boldStart = "<span class=\"bolded\">";
      boldEnd = "</span>";
    }
    
    
    
    if (servicePrice != null && servicePrice.length() != 0) {
      servicePrice = servicePrice.replace(".0", "");
      if (servicePrice.indexOf("zł") == -1) {
        servicePrice = servicePrice + " zł";
      }      
      writter.println("    <tr" + oddRowClass + "><td class=\"product\">" + boldStart + serviceName + boldEnd + "</td><td class=\"price\">" + servicePrice + "</td></tr>");
    } else {  
      writter.println("    <tr" + oddRowClass + "><td class=\"full\" colspan=\"2\">" + boldStart + serviceName + boldEnd + "</td></tr>");
    }    
  }
}
